<?php
/**
 * Project:     drylamp - rapid application development framework for php
 * File:        QueryWrapperTest.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * @copyright 2009-2010 Ilya St.
 * @author Ilya St. <provided at gmail dot com>
 * @version 1.0
 */


require_once 'PHPUnit/Framework.php';
require_once(dirname(__FILE__)."/../../autoload.php");

Conf::set( 'DB_HOST' , 'localhost' );
Conf::set( 'DB_NAME' , 'test_db' );
Conf::set( 'DB_USER' , 'test' );
Conf::set( 'DB_PASS' , 'test' );

class testtab extends ActiveRecord {}
class jointab extends ActiveRecord {}

/**
 * Class for testing private and protected methods of QueryWrapper
 * @author ilya
 */
class QWTestingExtension extends QueryWrapper {
	public function t_insertTokensInQuery($pSQL) {
		return $this->insertTokensInQuery($pSQL);
	}
	
	public function t_getWhereVars() {
		return $this->mWhereVars;
	}
	
	public function t_createWhereVars($pName) {
		$this->createWhereVar($pName);
	}
	
	public function t_makeDataObject($c) {
		return $this->makeDataObject($c);
	}
}

class QueryWrapperTest extends PHPUnit_Framework_TestCase {
	protected $doTearDown = true;
	
	protected function runQueryArr(Array $pQueryArr) {
		foreach($pQueryArr as $q) DBAdapter::$link->query($q);
    DBAdapter::$link->query("COMMIT");
	}	
	
	public function setUp() {
	  $query[] = "CREATE TABLE `testtab` (
							`tt_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
							`col1` VARCHAR( 255 ) NULL ,
							`col2` INT( 11 ) NULL ,
							`col3` TEXT NULL ,
							`col4` FLOAT( 10, 3 ) NULL) ENGINE = innodb;";
	  
	  $query[] = "CREATE TABLE `jointab` (
							`jt_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
							`jt1` VARCHAR( 255 ) NULL ,
							`jt2` INT( 11 ) NULL ,
							`jt3` TEXT NULL ,
							`jt4` FLOAT( 10, 3 ) NULL) ENGINE = innodb;";
	  
	  $query[] = "CREATE TABLE `jointab_noclass` (
							`jt_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
							`jt1` VARCHAR( 255 ) NULL ,
							`jt2` INT( 11 ) NULL ,
							`jt3` TEXT NULL ,
							`jt4` FLOAT( 10, 3 ) NULL) ENGINE = innodb;";
	 
	  $query[] ="INSERT INTO `testtab` (`col1` ,`col2` ,`col3` ,`col4`) VALUES (".
						  "'col1 content', '123', 'test test test dsfsfsdfsdf', '12.4');";
	  
 	  $query[] ="INSERT INTO `jointab` (`jt1` ,`jt2` ,`jt3` ,`jt4`) VALUES (".
						  "'jt111', '321', 'jt333', '0.5');";
				
		$this->runQueryArr($query);		
		$this->runQueryArr($query);		
	}
	
	public function tearDown() {
		if (!$this->doTearDown) return;
		$query[] = "DROP TABLE testtab";
		$query[] = "DROP TABLE jointab";
		$query[] = "DROP TABLE jointab_noclass";
		$this->runQueryArr($query);
	}
	
	
	public function testGetAssoc() {
	  $q = new QueryWrapper("SELECT * FROM testtab");
	  $q->execute();
	  $res = $q->getAssoc();
	  $compRes = array(
	  								0 => array('tt_id' => 1, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4),
	  								1 => array('tt_id' => 2, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4)	
	   								);
	  $this->assertTrue(is_array($res)); 								
    foreach($res as $key=>$row) {
    	$this->assertTrue(is_array($row));
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($res[$key][$vk],$compRes[$key][$vk]);
    	}
    } 
	}
	
	public function testGetExposables() {
		$q = new QueryWrapper("SELECT * FROM testtab");
		$q->execute();
	  $res = $q->getExposables();
	  $compRes = array(
	  								0 => array('tt_id' => 1, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4),
	  								1 => array('tt_id' => 2, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4)	
	   								);
	  $this->assertTrue(is_array($res)); 
	  foreach($res as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ExposableArray);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($res[$key]->$vk,$compRes[$key][$vk]);
    	}
    } 
	}
	
	public function testGetDataObjects() {
		$q = new QueryWrapper("SELECT * FROM testtab tt INNER JOIN jointab jt ON tt.tt_id=jt.jt_id");
		$q->execute();
		$dataObjects = $q->getDataObjects();
		$this->assertEquals(count($dataObjects), 2);
	  $compRes1 = array(
	  								0 => array('tt_id' => 1, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4),
	  								1 => array('tt_id' => 2, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4)	
	   								);
	   								
		$this->assertTrue(is_array($dataObjects));
		$arr = $dataObjects['testtab'];
		$this->assertTrue(is_array($arr));
		
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes1[$key][$vk]);
    	}
   	}
   	
   	$compRes2 = array(
	    							0 => array('jt_id' => 1, 'jt1' => "jt111", 'jt2' => 321 ,'jt3' => "jt333" ,'jt4' => 0.5),
	    							1 => array('jt_id' => 2, 'jt1' => "jt111", 'jt2' => 321 ,'jt3' => "jt333" ,'jt4' => 0.5)
	   								);
		
		$arr = $dataObjects['jointab'];
		$this->assertTrue(is_array($arr));
		
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes2[$key][$vk]);
    	}
   	}	  

   	//test with single parameter:
   	$dataObjects = $q->getDataObjects('testtab');
		$this->assertEquals(count($dataObjects), 1);
	   								
		$this->assertTrue(is_array($dataObjects));
		$arr = $dataObjects['testtab'];
		$this->assertTrue(is_array($arr));
		
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes1[$key][$vk]);
    	}
   	}
   	
   	//test with 2 parameters:
   	$dataObjects = $q->getDataObjects('testtab', 'jointab');
		$this->assertEquals(count($dataObjects), 2);
	   								
		$this->assertTrue(is_array($dataObjects));
		$arr = $dataObjects['testtab'];
		$this->assertTrue(is_array($arr));
		
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes1[$key][$vk]);
    	}
   	}
   	
		$arr = $dataObjects['jointab'];
		$this->assertTrue(is_array($arr));
		
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes2[$key][$vk]);
    	}
   	}
	}
	
	
	public function testGetDataObjectsByClass() {
		$q = new QueryWrapper("SELECT * FROM testtab tt INNER JOIN jointab jt ON tt.tt_id=jt.jt_id LIMIT 1");
		$q->execute();
		$arr = $q->getDataObjectsByClass('testtab');
		//array should be returned even if there is a single result in a set
		$this->assertTrue(is_array($arr));
		
		$q = new QueryWrapper("SELECT * FROM testtab tt INNER JOIN jointab jt ON tt.tt_id=jt.jt_id");
		$q->execute();
		$arr = $q->getDataObjectsByClass('testtab');
		$this->assertTrue(is_array($arr));
		
		$compRes1 = array(
	  								0 => array('tt_id' => 1, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4),
	  								1 => array('tt_id' => 2, 'col1' => "col1 content", 'col2' => 123 ,'col3' => "test test test dsfsfsdfsdf" ,'col4' => 12.4)	
	   								);
	   								
		foreach($arr as $key=>$exposable) {
    	$this->assertTrue($exposable instanceof ActiveRecord);
    	$row = $exposable->getFields();
    	foreach($row as $vk=>$val) {
    		$this->assertEquals($arr[$key]->$vk,$compRes1[$key][$vk]);
    	}
   	}	   								
	}
	
	public function testCreateWhereVars() {
		$q = new QWTestingExtension("");
		$vars = $q->t_getWhereVars();
		$this->assertFalse(array_key_exists('one_var', $vars));
		$q->t_createWhereVars('one_var');
		$vars = $q->t_getWhereVars();
		$this->assertTrue(array_key_exists('one_var', $vars));
	}
	
	public function testInsertTokensInQuery() {
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id={:tt_id:}");
		$whereVars = $q->t_getWhereVars();
		$this->assertTrue(array_key_exists('tt_id', $whereVars));
		$q->tt_id=123;
		$whereVars = $q->t_getWhereVars();
		$this->assertEquals($whereVars['tt_id']['value'], 123);
		
		//test invalid token:
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id={:1_tt_id:}");
		$whereVars = $q->t_getWhereVars();
		$this->assertFalse(array_key_exists('1_tt_id', $whereVars));
		
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id={:tt1id:}");
		$whereVars = $q->t_getWhereVars();
		$this->assertTrue(array_key_exists('tt1id', $whereVars));
		
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id={:tt_id:}");
		$q->tt_id = "test"; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id='test'", $q->getQuery());
		
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = 123; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>123", $q->getQuery());

		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = 12.5; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>12.5", $q->getQuery());
		
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = 0xFF; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>255", $q->getQuery());
		 
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = '123'; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>123", $q->getQuery());

		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = '123'; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>123", $q->getQuery());

		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = "12'3"; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>'12\'3'", $q->getQuery());

		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = null; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>NULL", $q->getQuery());

		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = false; 
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>FALSE", $q->getQuery());
		
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = "12'3"; 
		$this->assertEquals("12'3", $q->tt_id);
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>'12\'3'" , $q->getQuery());
		
		set_magic_quotes_runtime(1);
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = "12'3"; 
		$this->assertEquals("12'3", $q->tt_id);
		$this->assertEquals("SELECT * FROM testtab WHERE tt_id>'12\'3'" , $q->getQuery());
		
	}
	
	public function testMakeDataObject() {
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$obj = $q->t_makeDataObject('testtab');
		$this->assertTrue($obj instanceof testtab);
		
		$obj = $q->t_makeDataObject('jointab_noclass');
		$this->assertTrue($obj instanceof OnFly);
	}
	
	public function testSetGet() {
		$q = new QWTestingExtension("SELECT * FROM testtab WHERE tt_id>{:tt_id:}");
		$q->tt_id = 123;
		$vars = $q->t_getWhereVars();
		$this->assertEquals($vars['tt_id']['value'], 123);
		$this->assertEquals($q->tt_id, 123);
		
		$q->some_var = 321;
		$vars = $q->t_getWhereVars();
		$this->assertFalse(array_key_exists('some_var', $vars));
		$this->assertNull($q->some_var);
	}
}
